/* ************************************************************************ */
/*                                                                          */
/*  Copyright (c)2005 Russell Weir | damonsbane _at_ gmail                  */
/*                                                                          */
/* This library is free software; you can redistribute it and/or            */
/* modify it under the terms of the GNU Lesser General Public               */
/* License as published by the Free Software Foundation; either             */
/* version 3.0 of the License, or (at your option) any later version.       */
/*                                                                          */
/* This library is distributed in the hope that it will be useful,          */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of           */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        */
/* Lesser General Public License or the LICENSE file for more details.      */
/*                                                                          */
/* ************************************************************************ */

package tzparser;

class ZoneChangeSet {
	var changes : Array<ZoneChange>;
	var zonenames : Array<String>;

	public function new() {
		changes = new Array<ZoneChange>();
		zonenames = new Array<String>();
	}

	public function add(t:ZoneChange) : Void {
		changes.push(t);
		var have : Bool = false;
		for(i in zonenames) {
			if(i == t.name) {
				have = true;
				break;
			}
		}
		if(!have) {
			zonenames.push(t.name);
		}
	}

	function sort() {
	}

	/*
	public function toString() : String {
		sort();
		var sb = new StringBuf();
		var it = zonenames.iterator();
		sb.add("\t");
		sb.add("public static var abbrev : Array<String> = [");
		for(i in it) {
			sb.add(Parser.quotedString(i));
			if(it.hasNext())
				sb.add(", ");
		}
		//sb.add("];\n\tpublic static var changes : Array<ChangeRecord> = [\n");
		sb.add("];\n\tpublic static var changes : Array<ChangeRecord>;\n\n");
		sb.add("\tstatic function __init__() {\n\t\tchanges = [\n");
		var it2 = changes.iterator();
		for(i in it2) {
			i.setZoneNames(zonenames);
			sb.add(i);
			if(it2.hasNext())
				sb.add(",");
			sb.add(i.comment());
			sb.add("\n");
		}
		//sb.add("\t]; // end of change records\n");
		sb.add("\t\t];\n\t}\n");
		return sb.toString();
	}
	*/

	// Work around neko 1.6 MAX_STACK_PER_FUNCTION set to 128
	public function toString() : String {
		sort();
		var sb = new StringBuf();

		sb.add("\t\tabbrev = new Array();\n");
		var idx = 0;
		var count = 0;
		var start : Bool = true;
		while(idx < zonenames.length) {
			if(start) {
				sb.add("\t\tvar abbrev_"+Std.string(count)+" : Array<String> = [\n");
				start = false;
			}
			sb.add("\t\t\t");
			sb.add(Parser.quotedString(zonenames[idx]));
			if((idx%50 == 0 && idx != 0) || idx == zonenames.length -1) {
				sb.add("\n\t\t];\n");
				sb.add("\t\tabbrev = abbrev.concat(abbrev_"+Std.string(count)+");\n");
				count++;
				start = true;
			}
			else {
				sb.add(",\n");
			}
			idx++;
		}

		sb.add("\n\t\tchanges = new Array();\n");
		idx = 0;
		count = 0;
		start = true;
		while(idx < changes.length) {
			if(start) {
				sb.add("\t\tvar changes_"+Std.string(count)+" : Array<ChangeRecord> = [\n");
				start = false;
			}
			sb.add("\t");
			changes[idx].setZoneNames(zonenames);
			sb.add(changes[idx]);
			if((idx%50 == 0 && idx != 0) || idx == changes.length -1) {
				sb.add(changes[idx].comment());
				sb.add("\n\t\t];\n");
				sb.add("\t\tchanges = changes.concat(changes_"+Std.string(count)+");\n");
				count++;
				start = true;
			}
			else {
				sb.add(", ");
				sb.add(changes[idx].comment());
				sb.add("\n");
			}
			idx++;
		}

		return sb.toString();
	}
}